package cn.com.b2office.dao.hibernate;

import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import cn.com.b2office.dao.ShopDao;
import cn.com.b2office.domain.SCAssociation;
import cn.com.b2office.domain.Shop;

public class ShopDaoHibernateImpl extends HibernateDaoSupport implements
		ShopDao {

	public List<Shop> findAllShops() {
		List<Shop> list = null;
		Session s = getSession();
		try {
			Query query = s.createQuery("from Shop");
			query.setCacheable(true);
			list = query.list();
		} catch (RuntimeException e) {
			throw e;
		} finally {
			releaseSession(s);
		}
		return list;
	}

	public Shop getManageShop(int countyId) {
		Shop shop = null;
		Session s = getSession();
		try {
			Query query = s.createQuery("from SCAssociation");
			query.setCacheable(true);
			List<SCAssociation> list = query.list();
			if (list != null && list.size() > 0) {
				for (SCAssociation sc : list) {
					if (sc.getCounty() == countyId) {
						shop = (Shop) s.get(Shop.class, sc.getShop());
						break;
					}
				}
			}
		} catch (RuntimeException e) {
			throw e;
		} finally {
			releaseSession(s);
		}
		return shop;
	}

	public Shop getShop(int shopId) {
		return (Shop) getHibernateTemplate().get(Shop.class, shopId);
	}

}
